//
//  UILabel+YHExtension.swift
//  Adorbee
//
//  Created by amovision on 2024/11/26.
//

import UIKit
#if canImport(YYText)
import YYText
#endif
#if canImport(YYKit)
import YYKit
#endif

public extension UILabel {
  convenience init(_ text: Any? = nil, font: UIFont, color: UIColor, lines: Int = 1, aligment: NSTextAlignment = .left, scale: CGFloat = 1) {
    self.init()
    self.font = font
    self.textColor = color
    if let t = text as? String {
      self.text = t
    } else if let t = text as? AString {
      self.attributedText = t
    }
    numberOfLines = lines
    textAlignment = aligment
    adjustsFontSizeToFitWidth = true
    minimumScaleFactor = scale
  }
    
  convenience init(string: Any? = nil, lines: Int = 1, aligment: NSTextAlignment = .left, scale: CGFloat = 1) {
    self.init()
    if let t = string as? AString {
      self.attributedText = t
    }
    numberOfLines = lines
    textAlignment = aligment
    adjustsFontSizeToFitWidth = true
    minimumScaleFactor = scale
  }
    
  var textWidth: CGFloat {
    if let attributedText, attributedText.length > 0 {
      attributedText.width
    } else {
      text?.width(font) ?? 0
    }
  }
    
  func textHeight(_ width: CGFloat) -> CGFloat {
    if let attributedText, attributedText.length > 0 {
      attributedText.height(width)
    } else {
      text?.height(font, width: width) ?? 0
    }
  }
  
  var string: Any? {
    set {
      if let string = newValue as? String {
        self.text = string
      } else if let string = newValue as? AString {
        self.attributedText = string
      } else {
        self.text = nil
        self.attributedText = nil
      }
    }
    get {
      if let text { text }
      else if let attributedText { attributedText }
      else { nil }
    }
  }
}

public extension UITextView {
  convenience init(_ text: Any? = nil, font: UIFont, color: UIColor, lines: Int = 1, aligment: NSTextAlignment = .left) {
    self.init()
    if let text = text as? String {
      self.text = text
      self.font = font
    } else if let text = text as? AString {
      attributedText = text
    }
    textContainerInset = .zero
    contentInset = .zero
    textAlignment = aligment
  }
    
  var textWidth: CGFloat {
    if attributedText.length > 0 {
      attributedText.width
    } else {
      text.width(font)
    }
  }
    
  func textHeight(_ width: CGFloat) -> CGFloat {
    if attributedText.length > 0 {
      attributedText.height(width)
    } else {
      text.height(font, width: width)
    }
  }
  
  var string: Any? {
    set {
      if let string = newValue as? String {
        self.text = string
      } else if let string = newValue as? AString {
        self.attributedText = string
      } else {
        self.text = nil
        self.attributedText = nil
      }
    }
    get {
      if let text { text }
      else if let attributedText { attributedText }
      else { nil }
    }
  }
}

#if canImport(YYText) || canImport(YYKit)
public extension YYLabel {
  convenience init(_ text: Any? = nil, font: UIFont, color: UIColor, lines: Int = 1, aligment: NSTextAlignment = .left) {
    self.init()
    if let text = text as? String {
      self.text = text
      self.font = font
    } else if let text = text as? AString {
      attributedText = text
    }
    numberOfLines = UInt(lines)
    textAlignment = aligment
  }
    
  var textWidth: CGFloat {
    if let attributedText, attributedText.length > 0 {
      attributedText.width
    } else {
      text?.width(font) ?? 0
    }
  }
    
  func textHeight(_ width: CGFloat) -> CGFloat {
    if let attributedText, attributedText.length > 0 {
      attributedText.height(width)
    } else {
      text?.height(font, width: width) ?? 0
    }
  }

  var string: Any? {
    set {
      if let string = newValue as? String {
        self.text = string
      } else if let string = newValue as? AString {
        self.attributedText = string
      } else {
        self.text = nil
        self.attributedText = nil
      }
    }
    get {
      if let text { text }
      else if let attributedText { attributedText }
      else { nil }
    }
  }
}

public extension YYTextView {
  convenience init(_ text: Any? = nil, font: UIFont, color: UIColor, lines: Int = 1, aligment: NSTextAlignment = .left) {
    self.init()
    if let text = text as? String {
      self.text = text
      self.font = font
    } else if let text = text as? AString {
      attributedText = text
    }
    textContainerInset = .zero
    contentInset = .zero
    textAlignment = aligment
  }
    
  var textWidth: CGFloat {
    if let attributedText, attributedText.length > 0 {
      attributedText.width
    } else {
      text?.width(font) ?? 0
    }
  }
    
  func textHeight(_ width: CGFloat) -> CGFloat {
    if let attributedText, attributedText.length > 0 {
      attributedText.height(width)
    } else {
      text?.height(font, width: width) ?? 0
    }
  }
  
  var string: Any? {
    set {
      if let string = newValue as? String {
        self.text = string
      } else if let string = newValue as? AString {
        self.attributedText = string
      } else {
        self.text = nil
        self.attributedText = nil
      }
    }
    get {
      if let text { text }
      else if let attributedText { attributedText }
      else { nil }
    }
  }
}
#endif
